package common.activiti.web.action;

import java.util.List;

import org.activiti.engine.HistoryService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.history.HistoricActivityInstance;
import org.activiti.engine.history.HistoricDetail;
import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.history.HistoricProcessInstanceQuery;
import org.activiti.engine.history.HistoricVariableInstance;
import org.activiti.engine.repository.ProcessDefinition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import common.hibernate.vo.Page;
import common.web.action.CommonAction;
import common.web.vo.Result;

/**
 * 已归档流程实例
 */
@Controller
@RequestMapping(value = "/bpm/history/process")
public class HistoryProcessInstanceController extends CommonAction {

	@Autowired
	HistoryService historyService;

	@Autowired
	RepositoryService repositoryService;

	@Autowired
	RuntimeService runtimeService;

	/**
	 * 查询已结束流程实例
	 *
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "finished/list")
	@ResponseBody
	public Result finishedProcessInstanceList(@RequestBody JSONObject json) {
		Page<HistoricProcessInstance> page = new Page<HistoricProcessInstance>();
		page.setPageSize(super.getPageSize(json));
		page.setCurrentPage(super.getPageNumber(json));
		HistoricProcessInstanceQuery historicProcessInstanceQuery = historyService.createHistoricProcessInstanceQuery()
				.finished();
		List<HistoricProcessInstance> historicProcessInstances = historicProcessInstanceQuery.listPage(page.getStart(),
				page.getPageSize());

		// 查询流程定义对象
//		Map<String, ProcessDefinition> definitionMap = new HashMap<String, ProcessDefinition>();

//		for (HistoricProcessInstance historicProcessInstance : historicProcessInstances) {
//			definitionCache(definitionMap, historicProcessInstance.getProcessDefinitionId());
//		}
		Result res = Result.createPageResult(page);

		page.setRows(historicProcessInstances);
		page.setTotal(Long.valueOf(historicProcessInstanceQuery.count()).intValue());
//		mav.addObject("definitions", definitionMap);

		return res;
	}


	/**
	 * 查询历史相关信息
	 *
	 * @param processInstanceId
	 * @return
	 */
	@RequestMapping(value = "finished/view/{processInstanceId}")
	@ResponseBody
	public Result historyDatas(@PathVariable("processInstanceId") String processInstanceId) {

		List<HistoricActivityInstance> activityInstances = historyService.createHistoricActivityInstanceQuery()
				.processInstanceId(processInstanceId).list();

		// 查询历史流程实例
		HistoricProcessInstance historicProcessInstance = historyService.createHistoricProcessInstanceQuery()
				.processInstanceId(processInstanceId).singleResult();

		// 查询流程有关的变量
		List<HistoricVariableInstance> variableInstances = historyService.createHistoricVariableInstanceQuery()
				.processInstanceId(processInstanceId).list();

		List<HistoricDetail> formProperties = historyService.createHistoricDetailQuery()
				.processInstanceId(processInstanceId).formProperties().list();

		// 查询流程定义对象
		ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
				.processDefinitionId(historicProcessInstance.getProcessDefinitionId()).singleResult();

		Result r = new Result(historicProcessInstance);
		r.addMap("historicProcessInstance", historicProcessInstance);
		r.addMap("variableInstances", variableInstances);
		r.addMap("activities", activityInstances);
		r.addMap("formProperties", formProperties);
		r.addMap("processDefinition", processDefinition);

        return r;
	}

	/**
	 * 删除历史流程数据
	 *
	 * @param processInstanceId
	 * @return
	 */
	@RequestMapping(value = "finished/delete/{processInstanceId}")
	@ResponseBody
	public Result deleteProcessInstance(@PathVariable("processInstanceId") String processInstanceId) {
		historyService.deleteHistoricProcessInstance(processInstanceId);
		return Result.SUCCESS;
	}

//	/**
//	 * 流程定义对象缓存
//	 *
//	 * @param definitionMap
//	 * @param processDefinitionId
//	 */
//	private void definitionCache(Map<String, ProcessDefinition> definitionMap, String processDefinitionId) {
//		if (definitionMap.get(processDefinitionId) == null) {
//			ProcessDefinitionQuery processDefinitionQuery = repositoryService.createProcessDefinitionQuery();
//			processDefinitionQuery.processDefinitionId(processDefinitionId);
//			ProcessDefinition processDefinition = processDefinitionQuery.singleResult();
//
//			// 放入缓存
//			definitionMap.put(processDefinitionId, processDefinition);
//		}
//	}

}
